<?php

/**
 * contrato actions.
 *
 * @package    kernel
 * @subpackage contrato
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class contratoActions extends autocontratoActions
{

public function executeGenerarPdf()
  {
    $id = $this->getRequestParameter('id');
    $cont = $this->getTextoContrato($id);
    //$cont = str_replace("\n", "<br>", $cont);

	$id = $this->getRequestParameter('id');
	$pdf = new ReportePdf('P','mm');
	$pdf->SetAutoPageBreak(true,20);
	// settings
	$pdf->setLogo('/configuracion/logo-empresa.png');
	$pdf->SetFont("FreeSerif", "", 10);
	$pdf->SetTitulo("Contrato de Prestacion de Servicios");
	$pdf->Setempresa("5inco - ACC");
	$pdf->SetFillColor(225,225,225);
	$pdf->SetFechaConsulta(date('Y-m-d H:i'));
	$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
	$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
	$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
	$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
	$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
	$tmp = str_pad($id,19,'0',STR_PAD_LEFT);
	$pdf->setDocNumero($tmp);//$orden_de_servicio->getId());
	
	// init pdf doc
	$pdf->AliasNbPages();
	$pdf->AddPage();
    //$pdf->Cell(80, 10, "Hello World !!! & é € U û いろは");

	//$pdf->Ln(6);
	$y = $pdf->GetY();
	//$pdf->SetX(60);
	$pdf->Write(0,$cont,0,0,'J',0);
	//$pdf->Write(0, 5,$cont,0,0,'J',0);
	$pdf->Output();
   // $this->getResponse()->setContent($cont);		
  return sfView::NONE;
  }
  
  public function getTextoContrato($id)
  {
    $contratacion_contrato = ContratacionContratoPeer::retrieveByPK($id);
    $modelo = $contratacion_contrato->getContratacionModelo();	
    $texto = $modelo->getTexto();
    $variables = array();
    $valores = array();
    
    if($modelo)
	{
		$variables_modelo = $modelo->getContratacionModeloAVariablesJoinContratacionVariable();
		$variables_valores = $contratacion_contrato->getContratacionContratoAVariables();
		foreach($variables_modelo as $vm)
		{
			$v = $vm->getContratacionVariable();
			$variables[] = "<<".$v.">>";
			$valores[$v->getId()] = "";
		}
		foreach($variables_valores as $vv)
		{
			$valores[$vv->getVariableId()] = $vv->getValor();
		}
	}

	$configuracion = KernelConfiguracionPeer::getEtiquetasValorConfiguracion();	
	//var_dump($configuracion[0]);
	$texto = str_replace($configuracion[0], $configuracion[1] , $texto);

	$keys = array('<<Ti>>', '<<Identificacion>>', '<<RazonSocial>>','<<Tercero>>', '<<Email>>');
	$tercero = $contratacion_contrato->getKernelTercero();
	$array_vals= $tercero->toArrayPdf();


	$cont = str_replace($keys, $array_vals , $texto);
	$cont = str_replace($variables, $valores , $cont);
	return $cont; 
  }

  protected function saveContratacionContrato($contratacion_contrato)
  {

	if($contratacion_contrato->isNew())
	{
		$contratacion_contrato->setUsuarioId(Kernel::getUsuarioId());
		$contratacion_contrato->setFechaCreacion(date('Y-m-d H:i'));
	}

    $ret = parent::saveContratacionContrato($contratacion_contrato);
	if($ret )
	{
		$variables = $this->getRequestParameter('variables', array());
		foreach($variables as $key=>$val)
		{
			$c = new Criteria();
   			$c->add(ContratacionContratoAVariablePeer::VARIABLE_ID, $key);
			$c->add(ContratacionContratoAVariablePeer::CONTRATO_ID, $contratacion_contrato->getId());
			$cav = ContratacionContratoAVariablePeer::doSelectOne($c);
			if(!$cav)$cav = new ContratacionContratoAVariable();
			$cav->setVariableId($key);
			$cav->setContratoId( $contratacion_contrato->getId());
			$cav->setValor($val);
			$cav->save();
                        
		
		}
	}
	return $ret;
	
  }
  
  public function executeDeleteSelected()
  {
    $this->selectedItems = $this->getRequestParameter('sf_admin_batch_selection', array());

    try
    {		
      if(ContratacionContratoPeer::doDelete($this->selectedItems))
      	$this->getResponse()->setStatusCode('200');
      else
        $this->getResponse()->setStatusCode('204');

      return sfView::NONE;
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Contratacion contratos. Make sure they do not have any associated items.');
      $this->getResponse()->setStatusCode('404');
      return sfView::NONE;
    }


  }

  
}
